# -*- coding:utf-8 -*-
from STool.StockTool import StockTool
from STool.StockFileManager import StockFileManager

from STool.StockTT import *
from SDaily.StockSingle import StockSingle




class StockHtmlTool:
    

    def __init__(self,sql):
        self.mysql = sql
        self.rps_count =""


    @classmethod
    def buildCustomContent(self,title="1",content=""):
        stock_singleObjc = StockSingle()
        # f = open(stock_singleObjc.trade_stock_now_file_path, "w+") 
        if len(title) > 0:
             print("%s \n" %(title),file=stock_singleObjc.f)
        if len(content) > 0:
             print("%s \n" %(content),file=stock_singleObjc.f)

    @classmethod
    def buildStocklData(self,stocks_table_list, bankuai_mt_dic,rps_count,outTxt=True):

        rpstype = "rps"+str(rps_count)
        stock_codes=[]
        #这是一个元组
        for bk,item in stocks_table_list :
            
            bks = bk.split("-")
            value_bk = bks[0]
            if bks[0] != bks[1]:
                value_bk = value_bk+"-"+bks[1]
            
            if not bks[1] in bks[2]:
                value_bk = value_bk+"-"+bks[2]
                
            count = item['stock_count']
            stocks = item['stock_array']

            stock_name_array = [stock_dic['name']+"("+str(stock_dic[rpstype])+")" for stock_dic in stocks]
            stock_code_array = ["'"+stock_dic['symbol']+"'" for stock_dic in stocks]
            stock_codes.extend(stock_code_array)
            new_str = value_bk +":(共%s只): " %(count) + " ".join(stock_name_array) + " ||| " + " ".join(stock_code_array)
            if outTxt == True:
                self.buildCustomContent(new_str)

        stock_all_code_count = len(stock_codes)
        stock_all_code_str = " ".join(stock_codes)

        self.buildCustomContent("==  全部股票代码：%s 只  ==" %(str(stock_all_code_count)))
        self.buildCustomContent(stock_all_code_str)
        self.buildCustomContent("*********************************************************")
       
       
       
    @classmethod
    def buildJSDataToSQL(self,stocks_table_array,rpstype):
        sql_valid_data={}
        trade_date = stocks_table_array[0]['trade_date']
        sql_valid_data['trade_date']=trade_date
        sql_valid_data['rps_type']=rpstype

        valid_stocks =[]
        stocks_table_dic_bk= StockTool.remakeStocksGroupByBK(stocks_table_array,"hy")
        for bk,stocks in stocks_table_dic_bk.items() :
            if rpstype in stocks[0].keys():
                stocks = sorted(stocks, key=lambda book: book[rpstype], reverse=True) #根据rps降序
                    
            valid_stocks.extend(stocks)
        
        sql_valid_data['stocks']=valid_stocks
        StockFileManager.rewriteDataToFile(valid_stocks,rpstype+"_table")



    
    